package dse;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.Vector;
import java.util.Hashtable;

import dse.modules.eln.EarthLikeNetwork;

public class IOUtil 
{
	public static void NetworkToFile(EarthLikeNetwork eln, String file)
	{
		try
		{
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(eln);
			oos.close();
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
		}
	}
	
	public static EarthLikeNetwork NetworkFromFile(String file)
	{
		try
		{
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
			EarthLikeNetwork eln = (EarthLikeNetwork)ois.readObject();
			ois.close();
			return eln;
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
			return null;
		}
	}
	
	
	public static void DataObjectsToFile(Vector<DataObject> vd, String file)
	{
		try
		{
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(vd);
			oos.close();
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Vector<DataObject> DataObjectsFromFile(String file)
	{
		try
		{
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
			Vector<DataObject> vd = (Vector<DataObject>)(ois.readObject());
			ois.close();
			return vd;
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
			return null;
		}
	}
	
	public static void AccessRankingsToFile(Map<Cluster, DataObjectAccessRanking> ar, String file)
	{
		try
		{
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject((Hashtable<Cluster, DataObjectAccessRanking>)ar);
			oos.close();
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map<Cluster, DataObjectAccessRanking> AccessRankingsFromFile(String file)
	{
		try
		{
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
			Map<Cluster, DataObjectAccessRanking> ar = (Hashtable<Cluster, DataObjectAccessRanking>)(ois.readObject());
			ois.close();
			return ar;
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
			return null;
		}
	}
}
